﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.ShpMana
{
    public partial class wfVehicleEdit : msBsc.Cls.BPageHt
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void showVehicleType()
        {
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicleInfo = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                DataTable dt = vehicleInfo.GetVehicleType();
                if (dt == null || dt.Rows.Count < 1)
                {
                    return;
                }
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dt, "", "", 1, 100, "");
                this.ExecJScript(@"setSelect('vehicleType'," + retJson + ");");
            }
        }
        private void showVehicleBrand()
        {
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicleInfo = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                DataTable dt = vehicleInfo.GetVehicleBrand();
                if (dt == null || dt.Rows.Count < 1)
                {
                    return;
                }
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dt, "", "", 1, 100, "");
                this.ExecJScript(@"setSelect('vehicleBrand'," + retJson + ");");
            }
        }
        private void showVehicleColor()
        {
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicleInfo = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                DataTable dt = vehicleInfo.GetVehicleColor();
                if (dt == null || dt.Rows.Count < 1)
                {
                    return;
                }
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dt, "", "", 1, 100, "");
                this.ExecJScript(@"setSelect('vehicleColor'," + retJson + ");");
            }
        }
        static string vehicleImg = "";
        private void getVehicleInfo()
        {
            string vehicleId = this.GetParamHtValue("vehicleId");
            if (string.IsNullOrEmpty(vehicleId))
            {
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicleInfo = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                DataSet ds = vehicleInfo.GetNewVehicleInfo(vehicleId);
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    return;
                }
                vehicleImg = ds.Tables[0].Rows[0]["VEHICLE_DEFAULT_PIC"].ToString();
                string jsonRet = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 100, "");
                this.ExecJScript(@"setVehicleInfo(" + jsonRet + ")");
            }
        }

        private void updateVehicle()
        {
            string vehicleId = this.GetParamHtValue("vehicleId");//车辆Id
            string vehicleName = this.GetParamHtValue("vehicleName");//车辆名称
            string buyTime = this.GetParamHtValue("buyTime");//购买时间
            string seats = this.GetParamHtValue("seats");//座位数
            string price = this.GetParamHtValue("price");//价格
            string serviceArea = this.GetParamHtValue("serviceArea");//服务范围
            string vehicleType = this.GetParamHtValue("vehicleType");//车辆类型
            string vehicleBrand = this.GetParamHtValue("vehicleBrand");//车辆品牌
            string vehicleColor = this.GetParamHtValue("vehicleColor");//车辆颜色
            string serviceType = this.GetParamHtValue("serviceType");//服务类型
            string picture = this.GetParamHtValue("picture");//车辆图片
            string vehicleNumber = this.GetParamHtValue("vehicleNumber");//车牌号
            if (string.IsNullOrEmpty(vehicleId) &&
                string.IsNullOrEmpty(vehicleName) &&
                string.IsNullOrEmpty(buyTime) &&
                string.IsNullOrEmpty(seats) &&
                string.IsNullOrEmpty(price) &&
                string.IsNullOrEmpty(serviceArea) &&
                string.IsNullOrEmpty(vehicleType) &&
                string.IsNullOrEmpty(vehicleBrand) &&
                string.IsNullOrEmpty(vehicleColor) &&
                string.IsNullOrEmpty(serviceType) &&
                string.IsNullOrEmpty(vehicleNumber) &&
                string.IsNullOrEmpty(picture))
            {
                this.ExecJScript("layer.alert('车辆信息不完整', 3);");
            }
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicleInfo = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                if (picture!=vehicleImg)
                {
                    using (msBsc.AppLogic.DataAlc.CBizSysFile oCBizSysFile = new msBsc.AppLogic.DataAlc.CBizSysFile())
                    {
                        //保存到系统永久目录，之前上传时存到临时目录的，也可另写代码拷贝文件到其他目录
                        picture = oCBizSysFile.SaveImgToServer(""
                            , msBsc.SvRef.UserInfo.Operator
                            , "1"
                            , "VEH"
                            , ""
                            , picture//文件来源虚拟路径
                            , ""//文件来源物理路径，虚拟和物理只有一个就行
                            , true);
                    }
                }
                string strRet = vehicleInfo.UpdateNewVehicle(vehicleId, vehicleName, buyTime, seats, price, serviceArea, vehicleType, vehicleBrand, vehicleColor, serviceType, picture, vehicleNumber);
                if (strRet != "OK")
                {
                    this.Alert(strRet);
                    return;
                }
                this.ExecJScript(@"success();");
            }
        }

        public override void DoAction()
        {
            if (this.strAction=="init")
            {
                showVehicleBrand();
                showVehicleType();
                showVehicleColor();
                getVehicleInfo();
            }
            else if (this.strAction=="updateVehicle")
            {
                updateVehicle();
            }
        }
    }
}